$("<link>").attr({ 
	rel: "stylesheet",
    type: "text/css",
    href: "/public/wap/js/msgbox/style.css?" + Math.random()
}).appendTo("head");


function loading(){
	removeLoading();
	$("body").append("<div class='loading-cover' id='loading-cover'></div><div class='loading-box' id='loading'><div class='if if-min-logo icon-kemujiicon'></div><div class='loading'></div></div>");
	var left = ($(document).outerWidth()-$('#loading').outerWidth())/2;
	var top = ($(window).outerHeight()-$('#loading').outerHeight())/2;
	$("#loading").css({left:left+'px'});
	$("#loading").css({top:top+'px'});
}

function removeLoading(){
	$("#loading").remove();
	$("#loading-cover").remove();
}

function _confirm (msg, url, cont_id, btn_ok_name) {
	var opt_type    = cont_id ? 'delete' : '';
	var cont_id     = cont_id ? cont_id : '';
	var msg         = msg ? msg : '您确定要进行此操作吗？';
	var btn_ok_name = btn_ok_name ? btn_ok_name : '确定';

	html =  '<div class="aui-dialog">';
    html += '   <div class="aui-dialog-header">提示</div>';
    html += '    <div class="aui-dialog-body aui-text-left">' + msg + '</div>';
    html += '    <div class="aui-dialog-footer">';
    html += '        <div class="aui-dialog-btn aui-text-danger btn-cancel" tapmode>取消</div>';
    html += '        <div class="aui-dialog-btn aui-text-info btn-confirm" tapmode>' + btn_ok_name + '</div>';
    html += '    </div>';
    html += '</div>';
    html += '<div class="aui-mask"></div>';
    $("body").append(html);

    // 取消弹出提示
    $('.aui-dialog .btn-cancel').click(function() {
    	$('.aui-dialog').remove();
    	$('.aui-mask').remove();
    });
    // 确定弹出提示
	$('.aui-dialog .btn-confirm').click(function () {
		switch(opt_type){
			case 'delete':
				loading('正在删除中，请稍候...');
				$.ajax({
					type: "get",
					url: url,
					success : function(data){
						removeLoading();
						$('.aui-dialog').remove();
    					$('.aui-mask').remove();
						if(data.status==1){
							msgbox(data.info);
							$(cont_id).fadeOut('slow', function () {
								$(cont_id).remove();
							});
						}else{
							msgbox(data.info,'error');
						}
						// $.winRemove();
					},
					error :function (data) {
						removeLoading();
						$('.aui-dialog').remove();
    					$('.aui-mask').remove();
						msgbox('请求失败，请稍候再试！','error');
					}
				});
				break;
			default:
				if(url){
					jumpurl(url);
				}
		}
		$('.aui-dialog').remove();
    	$('.aui-mask').remove();
		return false;
	});
	return false;
}

/**
 * JS消息盒子
 * @param msg 信息
 * @param type 类型，有warn、success、error默认为success
 * @param redirect 重定向地址
 * @param timeout 缓冲时间，默认3秒
 * @return
 */
var msg_handle;
function msgbox(msg,type,redirect,timeout){
	var type = type ? type : 'success';
	var msg  = msg ? msg : '请求数据有误！';
	$(".mb-msgbox").remove();
	var left = ($(document).width()-240)/2;
	$("body").append("<div class='mb-msgbox'>"+msg+"</div>");
	var left = ($(document).outerWidth()-$('.mb-msgbox').outerWidth())/2;
	var top = ($(window).outerHeight()-$('.mb-msgbox').outerHeight())/2;
	$(".mb-msgbox").css({left:left+'px'});
	// $(".mb-msgbox").css({top:top+'px'});
	
	clearTimeout(msg_handle);
	timeout = timeout > 0 ? timeout : 3;
	msg_handle = setTimeout(function(){
		$(".mb-msgbox").remove();
		clearTimeout(msg_handle);
		if(redirect) document.location.href = redirect;
	},timeout*1000);
}
